<template>
	<view class="content">
		<!-- <ad adpid="1111111113" type="banner"></ad>
		<ad-common-rewarded ref="rewardedRef" @adEnd="onAdEnd" @adExit="onadExit" @adError="onAdError">
		</ad-common-rewarded>
		<ad-interstitial adpid="1111111113" :loadnext="true" ref="adInatance">
		</ad-interstitial> -->
		<uni-notice-bar text="连接 WiFi 功能需要打开 WiFi 开关,请提前打开。如果失败可前往设置页面直接连接。" />
		<!-- 		 <view class="item item2">
					<view class="left">
				<cloud-image v-if="hasLogin&&userInfo.data.avatar_file" :src="userInfo.data.avatar_file.url"
					width="120rpx" height="100%">
				</cloud-image>
				<uni-icons v-else custom-prefix="iconfont" type="icon-anonymous" size="60"></uni-icons>
			</view>
			<view class="left1">
				<view v-if="hasLogin">
					{{ userInfo.data.nickname}}
				</view>
				<view class="bottom">
					点击下方按钮安全连接 WiFi
				</view>
			</view>
						<view class="right">
				<uni-icons type="arrow-right" size="28" color="#999999"></uni-icons>
			</view>
		</view> -->
		<view class="item item2">
			<view class="left">
				<image src="/static/WIFI.png" mode="widthFix"
					style="width:120rpx;height: 100%;background-color: #1aad19"></image>
			</view>
			<view class="left1">
				<view>
					{{wifi.wifiName}}
				</view>
				<view class="bottom">
					安全连接
				</view>
			</view>
			<!-- 			<view class="right">
				<uni-icons type="arrow-right" size="28" color="#999999"></uni-icons>
			</view> -->
		</view>

		<!-- 如果自动连接的话那么就把这个放开 -->
		<!-- <view v-if="connectFail"> -->
		<!-- 连接成功后把按钮隐藏 -->
		<view>
			<!-- <button @click="getUserInfo(true)" type="primary">连接wifi</button> -->
			<button @click="clickConnectBtn()" type="primary" :disabled="connectedSuccess">连接wifi</button>
		</view>
	</view>
</template>

<script>
	import errorCode from "@/utils/errorCode.js"
	var http = require("../../utils/http.js");
	// import adci from "@/components/ad-common-interstitial/ad-common-interstitial.vue"
	// import adcr from "@/components/ad-common-rewarded/ad-common-rewarded.vue"
	export default {
		// components:{
		// 	adci,
		// 	adcr
		// },
		data() {
			return {
				// 表单数据
				wifi: {
					id: 0,
					storeName: "",
					userId: "",
					wifiName: "",
					wifiPwd: "",
				}
			}
		},
		onLoad(res) {

			var that=this
			if (res.wifiId) {
				uni.showLoading({
					title: '加载中'
				});
				console.log(res)
				var params = {
					url: "/p/wifi/info/" + res.wifiId,
					method: "PUT",
					callBack: res => {
						that.$data.wifi.id = res.id
						that.$data.wifi.storeName = res.storeName
						that.$data.wifi.userId = res.userId
						that.$data.wifi.wifiName = res.wifiName
						that.$data.wifi.wifiPwd = res.wifiPwd
						uni.hideLoading();
						console.log(that.$data.wifi.wifiName)
					}

				};
				http.request(params);
				// setTimeout(() => {
				// 	this.$refs.adInatance.show()
				// }, 1000)

			}
		},
		methods: {
			// clickConnectBtn: function() {
			// console.log("aa")
			// if (hasLogin.value) {
			// uni.showModal({
			// 	title: '连接wifi',
			// 	content: '观看一段小视频后可获得连接wifi的奖励，是否观看？',
			// 	success: function(res) {
			// 		if (res.confirm) {
			// 			// console.log('用户点击确定');
			// 			rewardedRef.value.show()
			// 		} else if (res.cancel) {
			// 			uni.showToast({
			// 				title: '抱歉，您放弃了',
			// 				duration: 2000
			// 			});
			// 		}
			// 	}
			// });
			// } else {
			// 	// getUserInfo(true)
			// 	loginByWeixin()
			// }
			// },
			clickConnectBtn() {
				// console.log("点击连接wifi")
				uni.getSystemInfo({
					success: (res) => {
						// console.log("获取系统信息成功")
						let system = ''
						// console.log("当前手机型号===>",res)
						if (res.platform == 'android') {
							system = parseInt(res.platform.substr(8))
						}
						if (res.platform == 'ios') {
							system = parseInt(res.platform.substr(4))
						}
						if (res.platform == 'android' && system < 6) {
							uni.showToast({
								title: '手机版本不支持',
								icon: 'none'
							})
							return
						}
						if (res.platform == 'ios' && system < 11.2) {
							uni.showToast({
								title: '手机版本不支持',
								icon: 'none'
							})
							return
						}
						//初始wifi模块
						this.start_wifi()
					}
				})
			},
			start_wifi() {
				uni.showLoading({
					mask: true,
					title: '正在启动wifi'
				});
				uni.startWifi({
					success: (res) => {
						// console.log("startWifi==>", res)
						uni.showToast({
							title: '初始化成功',
							icon: 'error'
						})
						this.connected()
					},
					fail: (error) => {
						// console.log("初始化失败", error)
						let errorMsg = "初始化失败"
						if (error.errCode && wifiErrorCode[error.errCode]) {

							errorMsg = wifiErrorCode[error.errCode]
						}
						uni.showModal({
							title: '提示',
							content: errorMsg || '连接失败',
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
						// setConnectData("fail")
						// connectFail.value = true
					}
				})
			},
			// 连接wifi
			connected() {
				console.log("正式连接wifi")
				//去保存用户点击数据
				var params = {
					url: "/p/wificonnect/save",
					method: "POST",
					data: {
						userId: uni.getStorageSync("userId"),
						userIdFather: this.wifi.userId,
						wifiId: this.wifi.id,
					},
					callBack: res => {
						console.log(res)
					}

				};
				http.request(params);
				uni.showLoading({
					title: '请稍等,连接中!',
				})
				uni.connectWifi({
					partialInfo: true,
					// SSID: wifiData.data.wifi_name, //Wi-Fi 设备名称
					// password: wifiData.data.password, //Wi-Fi 密码
					SSID: this.wifi.wifiName, //Wi-Fi 设备名称
					password: this.wifi.wifiPwd, //Wi-Fi 密码
					success(res) {
						// console.log("连接成功", res)
						uni.hideToast();
						uni.showModal({
							title: '提示',
							content: '连接成功',
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
						// connectFail.value = false
						// connectedSuccess.value = true

						// 连接成功后更新wifi连接成功次数
						// setConnectData("success")
						// wifiTable.where({
						// 	_id: wifiId.value
						// }).update({
						// 	connect_count: ++wifiData.data.connect_count
						// })
					},
					fail(error) {
						// setConnectData("fail")
						// console.log("连接失败", error)
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: '连接失败',
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
						// connectFail.value = true
					}
				})
			}

		}


	}
</script>

<style lang="scss">
	$itemWidth: 60px;

	.item {
		height: $itemWidth;
		padding: 16px 8px;
		background-color: #ffffff;
		margin: 32rpx 16rpx;
		border-radius: 10rpx;
	}

	.item2 {
		display: flex;
		flex-direction: row;
		align-items: flex-start;
		justify-content: space-between;

		.left {
			height: 100%;
		}

		.left1 {
			flex: 1;
			margin-left: 32rpx;
			font-size: 46rpx;

			.bottom {
				color: #999;
				font-size: 32rpx;
				margin-top: 5px;
			}
		}

		.right {
			line-height: $itemWidth;

		}
	}
</style>